package com.itheima.day04;

import java.util.Arrays;

public class Demo13 {
    public static void main(String[] args) {
        int[] arr = new int[] { 5, 3, 6, 2, 10, 2, 1 };
        Arrays.sort(arr);
        selectSort(arr);
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }

        System.out.println("========");
        int i = dichotomySearch(arr, 2);
        System.out.println("i = " + i);
    }


    private static void selectSort(int[] arr) {
        for (int i = 0; i < (arr.length - 1); i++) {
            int minIndex=i;
            for (int j = i+1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    minIndex=j;
                }
            }

            if (minIndex != i) {
                int temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i]=temp;
            }
        }
    }

    public static int dichotomySearch(int[] arr, int data) {
        int left=0;
        int right=arr.length - 1;
        while (left <= right) {
            int middleIndex=(left + right)/2;
            if (data > arr[middleIndex]) {
                left=middleIndex+1;
            } else if (data < arr[middleIndex]) {
                right=middleIndex-1;
            }else {
                return middleIndex;
            }
        }
        return -1;
    }
}
